package com.yusong.open.sdk.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by yusong on 2018/1/23.
 * 列表字符串转化工具类
 */
public class ListStrUtil {

    //分隔符
    private static final String Separator = "|";

    private static final String Separator_d = ",";

    public static boolean isEmpty(String str){
        return (str == null || str.length() == 0);
    }

    /*
    *列表转化为字符串
    */
    public static String list2Str(List<String> list){
        if(list == null || list.size() == 0){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for(String str : list){
            sb.append(str);
            sb.append(Separator);
        }
        String res = sb.toString().substring(0,sb.length()-1);
        return res;
    }

    /*
    *字符串转化为列表
    */
    public static List<String> str2List(String str){
        if(isEmpty(str)){
            return new ArrayList<String>();
        }
        String[] strs = str.split("\\|");
        List<String> list = new ArrayList<String>(strs.length);
        Collections.addAll(list, strs);
        return list;
    }

    /*
     *数组转化为字符串
     */
    public static String arr2Str(String[] arr){
        if(arr == null || arr.length == 0){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for(String str : arr){
            sb.append(str);
            sb.append(Separator_d);
        }
        String res = sb.toString().substring(0,sb.length()-1);
        return res;
    }

    /*
    *查询一个String中有多少个元素
    */
    public static Integer str2Num(String str){
        if(isEmpty(str)){
            return 0;
        }
        return str2List(str).size();
    }

    /*
    *浅复制集合
    */
    public static List<String> copy(List<String> list){
        if(list == null || list.size() == 0){
            return null;
        }
        List<String> result = new ArrayList<String>();
        result.addAll(list);
        return result;
    }


}
